package com.musollo;

import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * Class which handles the retreival of the configuration parameters from the
 * application properties file.
 * 
 * @author paw.janssen
 * 
 */
public final class Configuration {
	private static Configuration instance;

	private static final Logger LOGGER = Logger.getLogger(Configuration.class);

	private static final String DEFAULT_CONFIG_LOCATION = "/application.properties";

	/**
	 * Return a configuration instance with the default configuration.
	 * 
	 * @return - The default configuration.
	 */
	public static Configuration getInstance() {
		if (Configuration.instance == null) {
			Configuration.instance = new Configuration();
		} else {
			Configuration.instance.setConfigLocation(Configuration.DEFAULT_CONFIG_LOCATION);
		}

		return Configuration.instance;
	}

	/**
	 * Return a configuration instance loaded with the configuration from the
	 * specified configLocation.
	 * 
	 * @param configLocation
	 *            - A custom configLocation.
	 * @return - A custom configuration instance.
	 */
	public static Configuration getInstance(final String configLocation) {
		if (Configuration.instance == null) {
			Configuration.instance = new Configuration(configLocation);
		} else {
			Configuration.instance.setConfigLocation(configLocation);
		}

		return Configuration.instance;
	}

	private String configLocation;
	private String genPuidLocation;
	private String dnsKey;
	private String indexLocation;

	private String targetLocation;

	private boolean updateBeforeCategorize;

	private Configuration() {
		this(Configuration.DEFAULT_CONFIG_LOCATION);
	}

	private Configuration(final String configLocation) {
		this.configLocation = configLocation;
		this.readConfiguration();
	}

	public String getDnsKey() {
		return dnsKey;
	}

	public String getGenPuidLocation() {
		return genPuidLocation;
	}

	public String getIndexLocation() {
		return indexLocation;
	}

	public String getTargetLocation() {
		return targetLocation;
	}

	public boolean isUpdateBeforeCategorize() {
		return updateBeforeCategorize;
	}

	private void readConfiguration() {
		LOGGER.debug("Reading configuration: " + this.configLocation);
		Properties configurationProperties = new Properties();
		try {
			configurationProperties.load(Class.class.getResourceAsStream(configLocation));

			this.genPuidLocation = configurationProperties.getProperty("genpuid.location");
			this.dnsKey = configurationProperties.getProperty("genpuid.dns-key");
			this.indexLocation = configurationProperties.getProperty("application.indexLocation");
			this.targetLocation = configurationProperties.getProperty("application.targetLocation");

			int updateBeforeCategorize = Integer.parseInt(configurationProperties
					.getProperty("application.updatebeforecategorize"));
			if (updateBeforeCategorize == 1) {
				this.updateBeforeCategorize = true;
			} else {
				this.updateBeforeCategorize = false;
			}

		} catch (IOException e) {
			LOGGER.fatal("Cannot read configuration, exiting...");
			LOGGER.debug(e);
			System.exit(0);
		}
	}

	private void setConfigLocation(final String configLocation) {
		this.configLocation = configLocation;
		this.readConfiguration();
	}
}
